home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
msn
/
MSNCommands.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
14KB
|
423 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import logging
import msn
from rfc822 import Message as RfcMsg
from util import pythonize, Storage, isint, get, autoassign, fuzzydecode
class CommandProcessor(object):
def __init__(self, log):
if not log:
pass
self.log = logging.getLogger(type(self).__name__)
self.process_cmd = Storage(success = self.on_message, error = self.on_error)
def on_message(self, msg):
if self.log:
self.log.log_s(1, 'Got message: %r', str(msg)[:100].strip('\r\n'))
mname = 'recv_%s' % pythonize(msg.cmd)
getattr(self, mname, self.unknown_cmd)(msg)
def on_error(self, msg):
self.log.error('MSNError! %r', msg)
def unknown_cmd(self, msg):
self.log.warning('Received unknown message from transport. Message is <%r>', msg)
class MsgPayload(RfcMsg):
def __len__(self):
return len(self.body())
class Message(object):
argnames = []
def __init__(self, cmd, *args, **kws):
self.cmd = cmd.upper()
self.args = args
self.trid = int(kws.pop('trid', 0))
if not kws.pop('payload', None):
pass
self.payload = None
def length(self):
if self.is_payload:
return len(str(self.payload))
else:
return 0
length = property(length)
def is_trid(self):
if isint(self.trid):
pass
return bool(int(self.trid))
is_trid = property(is_trid)
def is_payload(self):
return self.payload is not None
is_payload = property(is_payload)
def is_error(self):
return bool(self.error_code)
is_error = property(is_error)
def error_str(self):
return msn.error_codes.get(self.error_code, 'Undocumented error')
error_str = property(error_str)
def error_code(self):
try:
return int(self.cmd)
except:
return None
error_code = property(error_code)
def __iter__(self):
return iter(self.args)
def __str__(self):
res = [
self.cmd]
if self.is_trid:
res.append(str(self.trid))
res.extend(map(str, self.args))
if self.is_payload:
res.append(str(self.length) + '\r\n' + str(self.payload))
return None + ' '.join(res) if not self.is_payload else ''
def __repr__(self):
err_str = None if self.is_error else ''
if type(self) is Message:
typestr = Message.__name__
else:
typestr = '%s(%s)' % (Message.__name__, type(self).__name__)
return None % ('<%s %s trid=%s, %sargs=%r, len(payload)=%s>', typestr, self.cmd, self.trid, err_str, self.args if self.is_payload else None)
def __getattr__(self, attr):
if attr in self.argnames:
return self.args[self.argnames.index(attr)]
else:
return object.__getattribute__(self, attr)
def get(self, key, default = sentinel):
try:
return self[key]
except KeyError:
if default is sentinel:
raise
else:
return default
except:
default is sentinel
def __getitem__(self, *a):
return self.args.__getitem__(*a)
def from_net(cls, data, is_payload):
if is_payload:
(cmdline, payload) = data.split('\r\n', 1)
else:
cmdline = data
payload = None
cmdline = cmdline.strip('\r\n ')
dlist = cmdline.split(' ')
cmd = dlist.pop(0)
if dlist and isint(dlist[0]):
trid = int(dlist.pop(0))
else:
trid = 0
if is_payload:
try:
l = dlist.pop()
except IndexError:
l = trid
trid = 0
try:
pass
except AssertionError:
dlist.append(l)
if not payload.strip():
pass
payload = None
except:
None<EXCEPTION MATCH>AssertionError
None<EXCEPTION MATCH>AssertionError
args = tuple(dlist)
if cmd == 'MSG':
subcmd = msn.util.msgtypes.get(MsgPayload(payload)['content-type'].split(';')[0], '')
cls = globals().get(cmd + subcmd, MSG)
else:
cls = globals().get(cmd, cls)
return cls(cmd, trid = trid, payload = payload, *args)
from_net = classmethod(from_net)
class OUT(Message):
argnames = [
'reason']
class MSG(Message):
argnames = 'name nick length'.split()
def __init__(self, *a, **k):
if a[0] != 'MSG':
a = ('MSG',) + a
Message.__init__(self, *a, **k)
self.type = msn.util.msgtypes.setdefault(self.payload['content-type'].split(';')[0], 'unknown')
def __getitem__(self, *a, **k):
try:
return Message.__getitem__(self, *a, **k)
except:
return self.payload.__getitem__(*a, **k)
def _get_payload(self):
return self._payload
def _set_payload(self, payload):
self._payload = MsgPayload(payload)
payload = property(_get_payload, _set_payload)
def from_net(self, data, is_payload):
data = data[4:]
return Message.from_net(data, is_payload)
def __repr__(self):
super = Message.__repr__(self)
return '%s type=%s>' % (super.rstrip('>'), self.type)
class MSGdatacast(MSG):
def _get_payload(self):
return MSG._get_payload(self)
def _set_payload(self, payload):
MSG._set_payload(self, payload)
self.contents = MsgPayload(self.payload.body())
payload = property(_get_payload, _set_payload)
def __getattr__(self, attr):
if attr in ('id', 'data'):
return get(self.contents, attr)
else:
return MSG.__getattr__(self, attr)
class UBM(Message):
def __init__(self, *a, **k):
Message.__init__(self, *a, **k)
self.type = msn.util.msgtypes.setdefault(self.payload['content-type'].split(';')[0], 'unknown')
def _get_payload(self):
return self._payload
def _set_payload(self, payload):
self._payload = MsgPayload(payload)
payload = property(_get_payload, _set_payload)
def __getitem__(self, *a, **k):
try:
return Message.__getitem__(self, *a, **k)
except:
return self.payload.__getitem__(*a, **k)
class MSNSB_Message(Message):
def __init__(self, contenttype, body, acktype = 'N', headers = { }, **moreheaders):
moreheaders.update(headers)
content = []
content.append('MIME-Version: 1.0')
content.append('Content-Type: text/x-%s' % contenttype)
for header in moreheaders.items():
content.append('%s: %s' % header)
content.append('')
payload = '\r\n'.join(content) + body
Message.__init__(self, 'MSG', acktype, payload = payload)
class MSNTextMessage(object):
def __init__(self, body, fontname = None, color = None, rightalign = False, bold = False, italic = False, underline = False, strike = False, charset = 0, family = 22):
autoassign(self, locals())
if isinstance(self.fontname, str):
self.fontname = self.fontname.decode('fuzzy utf8')
if isinstance(self.body, str):
self.body = self.body.decode('fuzzy utf8')
self.effects = ''
if self.bold:
self.effects += 'B'
if self.italic:
self.effects += 'I'
if self.underline:
self.effects += 'U'
if self.strike:
self.effects += 'S'
if self.color is not None:
(self.r, self.g, self.b) = self.color[:3]
else:
self.r = None
self.g = None
self.b = None
self._MSNTextMessage__html = u''
def __str__(self):
s = None([
None,
None,
u'\r\n'.join + 'MIME-Version: 1.0' + 'Content-Type: text/plain; charset=UTF-8' + 'X-MMS-IM-Format: ' if self.fontname is not None else '' if self.effects else '' if self.color else '' + 'CS=%(charset)d; PF=%(family)d',
'',
'%(body)s'])
return (s % vars(self)).encode('utf-8')
def from_net(cls, rfc822msg):
m = rfc822msg
fmt = m.get('X-MMS-IM-Format', '')
body = m.body().decode('fuzzy utf-8')
msg = cls(body)
if fmt:
fmt = msn.util.mime_to_dict(fmt)
_g = lambda k: get(fmt, k, None)
fontname = _g('FN')
msg.fontname = (None,) if fontname is None else fontname.decode('url').decode('utf-8')
color = _g('CO')
effects = _g('EF')
charset = _g('CS')
family = _g('PF')
ralign = _g('RL')
if color is not None:
try:
color = int(msn.util.bgr_to_rgb(color), 16)
except ValueError:
color = None
r = (color & 16711680) >> 16
g = (color & 65280) >> 8
b = color & 255
msg.color = (r, g, b)
msg.r = r
msg.g = g
msg.b = b
if charset is not None:
msg.charset = int(charset, 16)
if family is not None:
msg.family = int(family)
if ralign is not None:
msg.rightalign = bool(int(ralign))
if effects is not None:
ef = lambda x: x in effects
msg.bold = ef('B')
msg.italic = ef('I')
msg.underline = ef('U')
msg.strike = ef('S')
msg.effects = effects
return msg
from_net = classmethod(from_net)
def html(self):
if self._MSNTextMessage__html:
return self._MSNTextMessage__html
tag = tag
import util
t = tag(u'font')
if self.fontname:
t[u'face'] = self.fontname
if self.color:
t[u'color'] = u'#' + u''.join(map((lambda x: if not x:
pass'%02x' % 0), self.color[:3]))
if self.rightalign:
t[u'align'] = u'right'
innermost = top = t
for attr in ('bold', 'italic', 'underline', 'strike'):
if getattr(self, attr):
_tag = tag(attr[0])
innermost += _tag
innermost = _tag
continue
innermost._cdata = self.body
self._MSNTextMessage__html = top._to_xml(pretty = False).strip()
return self._MSNTextMessage__html